package com.kyzh.core.uis

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.gushenge.atools.util.AView
import com.kyzh.core.R
import com.kyzh.core.utils.dip


class SelectButton : LinearLayout {

    private var text = ""
    private var drawable: Int = R.drawable.arrow_down
    private var imageWidth: Float = 10f
    private var imageHeight: Float = 5f
    private var canRotate = false
    private lateinit var image: ImageView
    private lateinit var text1: TextView

    private var state = true
    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attributes: AttributeSet) : this(context, attributes, 0)
    constructor(context: Context, attributes: AttributeSet?, defStyle: Int) : super(context, attributes, defStyle) {
        val typeArray = context.obtainStyledAttributes(attributes, R.styleable.SelectButton)
        text = typeArray.getString(R.styleable.SelectButton_text) ?: ""
        drawable = typeArray.getResourceId(R.styleable.SelectButton_drawable, R.drawable.arrow_down)
        imageWidth = typeArray.getDimension(R.styleable.SelectButton_imageWidth, context.dip(6).toFloat())
        imageHeight = typeArray.getDimension(R.styleable.SelectButton_imageHeight, context.dip(6).toFloat())
        canRotate = typeArray.getBoolean(R.styleable.SelectButton_canRotate, false)
        initView(context)
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.select_button, this, true)
        text1 = findViewById(R.id.text)
        image = findViewById(R.id.image)

        text1.text = this@SelectButton.text
        AView.setWidth(image, imageWidth.toInt())
        AView.setHeight(image, imageHeight.toInt())
        image.setImageResource(drawable)


    }

    public fun start() {
        if (canRotate) ObjectAnimator.ofFloat(image, "rotation", -180f).setDuration(500).start()
        /*val color = resources.getColor(R.color.colorPrimary)
        image.drawable.setTint(color)
        text1.textColor = color*/
    }

    public fun reset() {
        if (canRotate) ObjectAnimator.ofFloat(image, "rotation", 0f).setDuration(500).start()
        /*val color = resources.getColor(R.color.font_33)
        image.drawable.setTint(color)
        text1.textColor = color*/
    }
    public fun setText(text: CharSequence) {
        text1.text = text.toString()
    }
}